package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.RoleDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckItemMapper;
import com.itheima.health.mapper.RoleMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Reference
    private PermissionService permissionService;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //  mp   配置插件：  完成分页 查询
        //   封装查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);
        Page<Role> page =null ;
       if(StringUtils.isEmpty(queryPageBean.getQueryString())){
            page = page(new Page<Role>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
       }else{
         //   select  * from  xxxx where  name like  xxx  limit ?,?
           queryWrapper.like("name",queryPageBean.getQueryString());
           page = page(new Page<Role>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
       }
        PageResult result = new PageResult(page.getTotal(),page.getRecords());
        return result;
    }

    @Override
    public void saveRole(RoleDTO roleDTO) {
        Integer id = roleDTO.getId();
        //  id   修改操作  值 不为null      insert  操作  为null
        if(!StringUtils.isEmpty(id)){
            //   修改操作   先删除  中间表数据
            baseMapper.deletePermissionIdsByRoleId(id);
        }
        //    检查组 信息录入  +  中间表数据录入
        saveOrUpdate(roleDTO);  //    checkGroupDTO 存在id   update 检查组表信息修改
        id  = roleDTO.getId();
        // 2  中间表录入数据
        for (Integer permissionId : roleDTO.getPermissionIds()) {
            baseMapper.addRoleIdAndPermissionId(id,permissionId);
        }


    }

    @Override
    public Map findpermissionInfoByRoleId(int id) {
        Map map  = new HashMap();
        List<Permission> list = permissionService.list();
        map.put("permissions",list);
        //  根据检查组id  去中间表查询 对应检查项ids
        int[]  permissionIds = baseMapper.findpermissionInfoByRoleId(id);
        map.put("permissionIds",permissionIds);
        return map;
    }

    @Override
    public List<Role> findAll() {
        //封装查询条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);
        List<Role> list = list(queryWrapper);
        return list;
    }

}



